<!--  -->
<template>
  <div class="login">
    <div class="login-box">
      <div class="avatar-box">
        <img src="../assets/img/beautiful.jpg" />
      </div>
      <el-form
        class="login-form"
        :model="form"
        :rules="rule"
        ref="loginFormref"
      >
        <el-form-item prop="us">
          <el-input
            prefix-icon="iconfont icon-denglu"
            v-model="form.us"
          ></el-input>
        </el-form-item>
        <el-form-item prop="ps">
          <el-input
            prefix-icon="iconfont icon-mima"
            type="password"
            v-model="form.ps"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login()">登录</el-button>
          <el-button type="info" @click="clean()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    //这里存放数据
    return {
      form: {
        us: "cao10",
        ps: "asd",
      },
      rule: {
        us: [
          { required: true, message: "请输入登录名称", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 3 到 10 个字符", trigger: "blur" },
        ],
        ps: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
          {
            min: 0,
            max: 10,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    clean() {
      //console.log(this)
      this.$refs.loginFormref.resetFields();
    },
    login() {
      this.$refs.loginFormref.validate((valid) => {
        if (!valid) return;
        let result = this.$http.post("/user/login", this.form).then((res) => {
          console.log(res.data);
          if (res.data.err == 0) {
            this.$message.success("登陆成功!");
            window.sessionStorage.setItem("token", res.data.token);
            this.$router.push("/home");
          } else {
            this.$message.error("登陆失败!");
          }
        });
      });
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
  deactivated() {},
};
</script>
<style lang="less" scoped>
.login {
  background: #2b4b6b;
  height: 100vh;
  .login-box {
    width: 450px;
    height: 300px;
    background: #fff;
    position: relative;
    margin: auto;
    top: 200px;
  }
}
.avatar-box {
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 130px;
  height: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  background: #fff;
}
.avatar-box img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: #fff;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
.login-form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 10px;
  box-sizing: border-box;
}
</style>